/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.uit.ecommerce.access.mapper;

import com.uit.ecommerce.dto.CountryDTO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author ngloc_it
 */
public class CountryMapper extends MapperDB{
    public CountryMapper() throws Exception{
        super();
    }
     private void InitCountryDTOFromRs(CountryDTO country, ResultSet rs) throws Exception{
        if((rs != null) && (country != null)){
            country.setId(rs.getString("MaNuoc"));
            country.setName(rs.getString("TenNuoc"));
        }
    }
      public ArrayList<CountryDTO> GetAllCountry() throws Exception{
        ArrayList<CountryDTO> listResult = new ArrayList<CountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.datnuoc");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                CountryDTO temp = new CountryDTO();
                InitCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
       public ArrayList<CountryDTO> GetCountryName(String country) throws Exception{
        ArrayList<CountryDTO> listResult = new ArrayList<CountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.datnuoc where TenNuoc='"+country+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                CountryDTO temp = new CountryDTO();
                InitCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
       public CountryDTO GetCountryID(String ID) throws Exception{
        CountryDTO Result = new CountryDTO();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.datnuoc where MaNuoc='"+ID+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                InitCountryDTOFromRs(Result, rs);
             }
        }catch(Exception ex){
            throw ex;
        }

        return Result;
    }
public void insertCountry(CountryDTO cls) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Insert into ecormerce.datnuoc values('"+cls.getId()+"','"+cls.getName()+"')");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public void DeleteCountry(String Id) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Delete from ecormerce.datnuoc where MaNuoc='");
            sql.append(Id).append("'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public boolean  checkCountryExit(String Id) throws Exception{
     try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.datnuoc where MaNuoc='"+Id+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           if((rs != null) && (rs.next())){
                return true;
            }
        }catch(Exception ex){
            throw ex;
        }
     return  false;
}
}
